document.addEventListener('DOMContentLoaded', function() {
  // 单文件上传
  const singleUploadForm = document.getElementById('singleUploadForm');
  const singleFileResult = document.getElementById('singleFileResult');
  
  // 多文件上传
  const multipleUploadForm = document.getElementById('multipleUploadForm');
  const multipleFileResult = document.getElementById('multipleFileResult');
  
  // 文件列表
  const refreshFilesBtn = document.getElementById('refreshFiles');
  const filesList = document.getElementById('filesList');
  
  // 单文件上传处理
  singleUploadForm.addEventListener('submit', function(e) {
    e.preventDefault();
    
    const fileInput = document.getElementById('singleFile');
    const file = fileInput.files[0];
    
    if (!file) {
      showMessage(singleFileResult, '请选择一个文件', 'error');
      return;
    }
    
    const formData = new FormData();
    formData.append('file', file);
    
    uploadFile('/upload', formData, singleFileResult);
  });
  
  // 多文件上传处理
  multipleUploadForm.addEventListener('submit', function(e) {
    e.preventDefault();
    
    const fileInput = document.getElementById('multipleFiles');
    const files = fileInput.files;
    
    if (files.length === 0) {
      showMessage(multipleFileResult, '请至少选择一个文件', 'error');
      return;
    }
    
    const formData = new FormData();
    for (let i = 0; i < files.length; i++) {
      formData.append('files', files[i]);
    }
    
    uploadFile('/upload-multiple', formData, multipleFileResult);
  });
  
  // 刷新文件列表
  refreshFilesBtn.addEventListener('click', function() {
    fetchFilesList();
  });
  
  // 页面加载时获取文件列表
  fetchFilesList();
  
  // 上传文件函数
  function uploadFile(url, formData, resultElement) {
    resultElement.innerHTML = '上传中...';
    resultElement.className = 'result';
    
    fetch(url, {
      method: 'POST',
      body: formData
    })
    .then(response => {
      if (!response.ok) {
        throw new Error('上传失败');
      }
      return response.json();
    })
    .then(data => {
      showMessage(resultElement, data.message, 'success');
      fetchFilesList(); // 上传成功后刷新文件列表
    })
    .catch(error => {
      showMessage(resultElement, error.message, 'error');
    });
  }
  
  // 获取文件列表
  function fetchFilesList() {
    filesList.innerHTML = '加载中...';
    
    fetch('/files')
      .then(response => {
        if (!response.ok) {
          throw new Error('获取文件列表失败');
        }
        return response.json();
      })
      .then(data => {
        if (data.files && data.files.length > 0) {
          filesList.innerHTML = '';
          data.files.forEach(file => {
            const li = document.createElement('li');
            li.textContent = file;
            filesList.appendChild(li);
          });
        } else {
          filesList.innerHTML = '<li>没有上传的文件</li>';
        }
      })
      .catch(error => {
        filesList.innerHTML = `<li class="error">错误: ${error.message}</li>`;
      });
  }
  
  // 显示消息函数
  function showMessage(element, message, type) {
    element.textContent = message;
    element.className = `result ${type}`;
  }
}); 